/**
 * Created by tdzl2003 on 10/16/16.
 */

import {List} from 'immutable';

var BlockTree = require('draft-js/lib/BlockTree');

var originGenerate = BlockTree.generate;

BlockTree.generate = function generate(block, decorator) {
  var textLength = block.getLength();
  const tree = originGenerate(block,decorator);
  const last = tree.last();
  const someLeaf = last.get('leaves').get('0');
  const lastDecoratorKey = last.get('decoratorKey');
  const lastDecoratorComponent = lastDecoratorKey && decorator.getComponentForKey(lastDecoratorKey);
  if (lastDecoratorComponent && lastDecoratorComponent.isAtomic) {
    return tree.push(last.merge({
      start: textLength,
      end: textLength,
      decoratorKey: null,
      leaves: List.of(someLeaf.merge({ start: textLength, end: textLength }))
    }));
  }
  return tree;
}